//
//  LLScreenItemView.m
//  TaoTuo
//
//  Created by LG on 2020/11/18.
//  Copyright © 2020 刘广. All rights reserved.
//

#import "LLScreenItemView.h"
#import "LLScreenItemCollectionViewCell.h"

@interface LLScreenItemView ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray *selectedArray;

@end

@implementation LLScreenItemView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.selectedArray = [NSMutableArray array];
        [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self);
        }];
        
    }
    return self;
}

- (void)setModel:(LLScreenSectionModel *)model
{
    _model = model;
    
    [self.selectedArray removeAllObjects];
    
    self.collectionView.allowsMultipleSelection = model.allowMulti;
    
    [self.collectionView reloadData];
    
//    if (!self.collectionView.allowsMultipleSelection) {
        for (LLScreenItemModel *model in _model.dataArray) {
            if (model.isSelected) {
                NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[_model.dataArray indexOfObject:model] inSection:0];
                [self.collectionView selectItemAtIndexPath:indexPath animated:YES scrollPosition:UICollectionViewScrollPositionNone];
                break;
            }
        }
//    }
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.model.dataArray.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    LLScreenItemModel *model = self.model.dataArray[indexPath.row];
    LLScreenItemCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.model = model;
    return cell;
}


- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    LLScreenItemModel *itemModel = self.model.dataArray[indexPath.row];
    
    return itemModel.isEnable;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    LLScreenItemModel *itemModel = self.model.dataArray[indexPath.row];
    
    if (!self.model.allowMulti && self.model.allowCancelSelect) {
        // 单选，能取消选中
        itemModel.isSelected = !itemModel.isSelected;
        for (LLScreenItemModel *selectModel in self.selectedArray) {
            selectModel.isSelected = NO;
        }
        [self.selectedArray removeAllObjects];
    } else {
        if (self.model.allowMulti || self.model.allowCancelSelect) {
            // 可以多选或者能取消选中
            itemModel.isSelected = !itemModel.isSelected;
        } else {
            // 单选不能取消选中
            itemModel.isSelected = YES;
        }
    }
    
    if (itemModel.isSelected && ![self.selectedArray containsObject:itemModel]) {
        [self.selectedArray addObject:itemModel];
    }
    if (!itemModel.isSelected) {
        [self.selectedArray removeObject:itemModel];
    }
    
    if (!self.model.allowMulti && self.model.allowCancelSelect) {
        
        [self.collectionView reloadData];
        
    }
    
    if (self.model.selectedItemBlock) {
        self.model.selectedItemBlock(self.selectedArray);
    }
    
}

- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath
{
    LLScreenItemModel *itemModel = self.model.dataArray[indexPath.row];
    itemModel.isSelected = NO;
    [self.selectedArray removeObject:itemModel];
    
    if (self.model.allowMulti || self.model.allowCancelSelect) {
        // 可以多选或者能取消选中
        if (self.model.selectedItemBlock) {
            self.model.selectedItemBlock(self.selectedArray);
        }
    }
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    LLScreenItemModel *itemModel = self.model.dataArray[indexPath.row];
    if (!CGSizeEqualToSize(self.model.minSize, CGSizeZero)) {
        return self.model.minSize;
    }
    
    CGFloat w = [itemModel.title boundingRectWithSize:CGSizeMake(MAXFLOAT, 30) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:13]} context:nil].size.width + 5;
    w = w < 65 ? 65 : w;
    return CGSizeMake(w, 30);
    
}



- (UICollectionView *)collectionView
{
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.minimumLineSpacing = 10;
        layout.minimumInteritemSpacing = 10;
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        layout.sectionInset = UIEdgeInsetsMake(0, 10, 0, 10);
        
        _collectionView = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:layout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.backgroundColor = UIColor.whiteColor;
        _collectionView.showsHorizontalScrollIndicator = NO;
        [_collectionView registerClass:[LLScreenItemCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
        [self addSubview:_collectionView];
        
    }
    return _collectionView;
}




@end
